﻿using SPSAndong.Business.Dtos.DeviceExceptionMonitorDtos;
using SPSAndong.Business;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SPSAndong.Forms
{
    public partial class FrmExceptionRecord : UIPage
    {
        DeviceExceptionMonitorBLL deviceExceptionMonitorBLL = new DeviceExceptionMonitorBLL();
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;
        /// <summary>
        /// 数据表每页行数
        /// </summary>
        int pageSize = 30;
        public FrmExceptionRecord()
        {
            InitializeComponent();
        }

        private void FrmExceptionRecord_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;

            //设置开始时间为一星期前
            this.dpStartDate.Value = DateTime.Now.AddDays(-2);
            this.dpEndDate.Value = DateTime.Now;


            isLoad = false;
            uiPagination1.Style = UIStyle.Blue;
            uiPagination1.ShowJumpButton = true;

            dgvHistory.AutoGenerateColumns = false;
            dgvHistory.AllowUserToAddRows = false;
            dgvHistory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHistory.ReadOnly = true;
            dgvHistory.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dgvHistory.AddColumn("设备ID", "DeviceId", 10);
            dgvHistory.AddColumn("设备名称", "DeviceName", 10);
            dgvHistory.AddColumn("设备类型", "DeviceTypeName", 10);
            dgvHistory.AddColumn("设备IP", "DeviceIP", 15);
            dgvHistory.AddColumn("异常描述", "ExceptionDescription", 40);
            dgvHistory.AddColumn("异常时间", "ExceptionDate", 15);

            Search();
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            var queryDto = GetQueryDto();
            queryDto.PageIndex = pageIndex;
            queryDto.PageSize = count;
            dgvHistory.DataSource = deviceExceptionMonitorBLL.GetModelListBySql(queryDto);
            dgvHistory.Refresh();
        }


        private void btnQuery_Click(object sender, EventArgs e)
        {
            Search();
        }

        /// <summary>
        /// 查询
        /// </summary>
        private void Search()
        {
            var queryDto = GetQueryDto();
            uiPagination1.TotalCount = deviceExceptionMonitorBLL.GetModelCountByWhere(queryDto);
            uiPagination1.ActivePage = 1;
            uiPagination1_PageChanged(null, null, 1, pageSize);
        }

        private QueryDeviceExceptionMonitorDto GetQueryDto()
        {
            var deviceType = -1;//设备类型 -1:全部,0:PLC,1:LIP,2:UPS,3:I/O箱,4:交换机,5:安东屏,6:喇叭
            switch (cbType.Text)
            {
                case "PLC":
                    deviceType = 0;
                    break;
                case "LIP":
                    deviceType = 1;
                    break;

                case "UPS":
                    deviceType = 2;
                    break;
                case "I/O箱":
                    deviceType = 3;
                    break;

                case "交换机":
                    deviceType = 4;
                    break;
                case "安东屏":
                    deviceType = 5;
                    break;
                case "喇叭":
                    deviceType = 6;
                    break;
            }
            var queryDto = new QueryDeviceExceptionMonitorDto()
            {
                DeviceName = txtDeviceName.Text,
                DeviceType = deviceType,
                DeviceIP = txtDeviceIP.Text,
                DateStart = dpStartDate.Value,
                DateEnd = dpEndDate.Value,
                PageIndex = 1,
                PageSize = pageSize,
            };
            return queryDto;
        }
    }
}
